package com.springcloud.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class HomeService {

    @Autowired
    private RestTemplate restTemplate ;

    /**
     * 请求eurekaClient中注册的服务 /home?name=xxx
     * @HystrixCommand 注解 fallbackMethod属性，指定断路后的处理函数
     */
    @HystrixCommand(fallbackMethod="homeError")
    public String homeSer(String name) {
        // 调用时直接使用注册的服务端名称 SERVICE-HOME/接口名称
        return restTemplate.getForObject("http://SERVICE-HOME/home?name="+name,String.class);
    }

    public String homeError(String name) {
        return "Hello,"+name+",sorry,error!";
    }

}
